﻿Module Common

    Public Function TabSequence(ByVal NumberOfTabs As Integer) As String

        Dim sb As Text.StringBuilder

        sb = New Text.StringBuilder
        While NumberOfTabs > 0
            sb.Append(vbTab)
            NumberOfTabs = NumberOfTabs - 1
        End While
        Return sb.ToString

    End Function

    Public Function FormatStringText(ByVal s As String) As String

        Dim sb As Text.StringBuilder
        Dim i As Integer
        Dim c As Char
        Dim Stage As Integer

        sb = New Text.StringBuilder
        i = s.IndexOf("(Text)")
        If i >= 0 Then
            i = i + 6
            Stage = 0
            While i < s.Length
                c = s.Chars(i)
                Select Case Stage
                    Case 0
                        If AscW(c) >= 32 Then
                            sb.Append(c)
                            Stage = 1
                        End If
                    Case 1
                        If AscW(c) >= 32 Then
                            sb.Append(c)
                        ElseIf AscW(c) = 9 Then
                            sb.Append("|")
                        End If
                End Select
                i = i + 1
            End While
        End If
        If sb.Length = 0 Then
            sb.Append("|")
        End If
        Return sb.ToString

    End Function

    Public Function ValidXMLText(ByVal s As String, ByVal DoubleQuotesAsSingle As Boolean) As String

        Dim res As Text.StringBuilder
        Dim c As Char

        If DoubleQuotesAsSingle Then
            s = s.Replace("""""", """")
        End If
        res = New Text.StringBuilder
        For Each c In s
            If c = "&" Then
                res.Append("&amp;")
            ElseIf c = "<" Then
                res.Append("&lt;")
            ElseIf c = ">" Then
                res.Append("&gt;")
            ElseIf c = "'" Then
                res.Append("&apos;")
            ElseIf c = """" Then
                res.Append("&quot;")
            ElseIf (AscW(c) <= 32) OrElse (AscW(c) > 127) Then
                res.Append("&#")
                res.Append(AscW(c))
                res.Append(";")
            Else
                res.Append(c)
            End If
        Next
        Return res.ToString

    End Function

    Public Function RemoveStartEndQuotesFromValidXML(ByVal txt As String) As String

        If txt.StartsWith("&quot;") Then
            txt = txt.Substring("&quot;".Length)
        End If
        If txt.EndsWith("&quot;") Then
            txt = txt.Substring(0, txt.Length - ("&quot;".Length))
        End If
        Return txt

    End Function

    Public Function ConvertColor(ByVal c As String) As String

        If c.Contains("&H") Then
            c = c.Trim.Replace("&H", "").Replace("&", "")
        Else
            c = Hex(Convert.ToInt32(c, Globalization.CultureInfo.InvariantCulture))
        End If
        While c.Length < 6
            c = "0" & c
        End While
        If c.Length > 6 Then
            c = c.Substring(c.Length - 6)
        End If
        c = "#" & c.Substring(4, 2) & c.Substring(2, 2) & c.Substring(0, 2)
        Return c

    End Function

    Public Function ConvertBoolean(ByVal b As String) As String

        If Convert.ToInt32(b, Globalization.CultureInfo.InvariantCulture) = 0 Then
            Return "False"
        Else
            Return "True"
        End If

    End Function

    Public Sub CopyElement(ByVal Key As String, ByVal SourceElementInfo As Dictionary(Of String, String), ByVal TargetElementInfo As Dictionary(Of String, String))

        If SourceElementInfo.ContainsKey(Key) Then
            TargetElementInfo.Add(Key, SourceElementInfo(Key))
        End If

    End Sub

End Module
